//
//  RickyRefreshGifView.m
//  Ricky_RefreshControl
//
//  Created by MR-zhang on 15/7/8.
//  Copyright (c) 2015年 Ricky. All rights reserved.
//

#import "RickyRefreshGifView.h"

static CGFloat const gifPathLength = 200;// 整个动画路径长度
static NSString *const gifAnimationKey = @"gifAnimationKey";

@interface RickyRefreshGifView ()
@property (nonatomic, strong) UIImageView *gifImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation RickyRefreshGifView

- (id)initWithRefreshViewType:(RickyRefreshViewType)viewType
{
    self = [super initWithRefreshViewType:viewType];
    if (self) {
        
    }
    return self;
}

#pragma mark -- 
#pragma mark -- setter\getter
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = ({
            UILabel *label = [[UILabel alloc] init];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:RickyHeaderTitleFontSize];
            label.textColor = [UIColor blackColor];
            label.textAlignment = 1;
            label;
        });
        
        [self insertSubview:_titleLabel atIndex:0];
    }
    return _titleLabel;
}

- (UIImageView *)gifImgView
{
    if (!_gifImgView) {
        _gifImgView = ({
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.animationDuration = RickyGifAnimationDuration;
            imageView;
        });
        [self addSubview:_gifImgView];
    }
    return _gifImgView;
}

- (void)setPlaceHolderImage:(UIImage *)placeHolderImage
{
    if (_placeHolderImage != placeHolderImage) {
        _placeHolderImage = placeHolderImage;
        
        self.gifImgView.image = _placeHolderImage;
    }
}

- (void)setTitle:(NSString *)title
{
    if (title.length == 0) {
        return;
    }

    _title = title;
    self.titleLabel.text = _title;
}

- (void)setAnimationImages:(NSArray *)animationImages
{
    if (animationImages.count > 0) {
        _animationImages = animationImages;
        self.gifImgView.animationImages = _animationImages;
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.frame = self.bounds;
    
    // 整条动画路径在中间, gifImgView在路径的侧
    CGSize imageSize = self.placeHolderImage.size;
    CGFloat imageScale = 0.6;
    self.gifImgView.frame = CGRectMake((self.frame.size.width-gifPathLength)/2, (self.frame.size.height-imageSize.height*imageScale)/2, imageSize.width*imageScale, imageSize.height*imageScale);
  
}

- (void)startAnimation
{
    self.titleLabel.hidden = YES;
    [self.gifImgView startAnimating];
    CABasicAnimation *maskAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
    maskAnimation.byValue = @(gifPathLength);
    maskAnimation.repeatCount = INT_MAX;
    maskAnimation.duration = RickyGifMoveAnimationDuration;
    [self.gifImgView.layer addAnimation:maskAnimation forKey:gifAnimationKey];
}

#pragma mark --
#pragma mark -- 重写父类属性
- (void)setRefreshState:(RickyRefreshState)refreshState
{
    [super setRefreshState:refreshState];
    if (refreshState == RickyRefreshStateRefreshing) {
        [self startAnimation];
    }
}

- (void)setCompleteType:(RickyRefreshCompleteType)completeType
{
    [super setCompleteType:completeType];

    __weak __typeof(self)weakSelf = self;
    [self setRefreshState:RickyRefreshStateNormal withCallBack:^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf.gifImgView stopAnimating];
        strongSelf.titleLabel.hidden = NO;
        [strongSelf.gifImgView.layer removeAnimationForKey:gifAnimationKey];
    }];
}

#pragma mark -- 添加从后台返回通知，保证动画正常进行不会停止
- (void)reloadNotification:(NSNotification *)notification
{
    if (self.refreshState == RickyRefreshStateRefreshing) {
        [self startAnimation];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
